import request from '@/router/axios';
// import * as qs from 'qs'

const api = {
    addWorkflow: "/api/automations/save",
    deleteWorkflow: "/api/automations/delete",
    updateWorkflow: "/api/automations/update",
    getWorkflowDetail: "/api/automations/detail",
    workflowList: "/api/automations/list",
    definitions: "/api/automations/definitions",
    nodeOutput: "/api/automations/node-output",
}

export function addWorkflow(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.addWorkflow,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("添加工作流失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function deleteWorkflow(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.deleteWorkflow + `?id=${id}`,
            method: 'post'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.success)
            }else{
                reject(new Error("删除工作流失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateWorkflow(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateWorkflow,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("修改工作流失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getWorkflowDetail(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.getWorkflowDetail + `?id=${id}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到工作流详情"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAppWorkflowList(appId) {
    return new Promise((resolve, reject) => {
        request({
            url: api.workflowList + `?appId=${appId}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到工作流列表"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAppWorkflowNodes() {
    return new Promise((resolve, reject) => {
        request({
            url: api.definitions,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到工作流结点信息"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getNodeOutput(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.nodeOutput + `?automationId=${data.automationId}&nodeId=${data.nodeId}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                resolve([])
            }
        }).catch((err) => {
            resolve([])
        });
    })
}